clc
clear all

N = 128;
N2 = N/2;
filterLen = 16;

w =  dbaux(filterLen);
[lpd,hpd,lpr,hpr] = orthfilt(w); 
    
[Fm W W2] = creatingFilterMatrix(N,filterLen);
Fnt = createFn(N);
norm(Fnt -Fm)
Fnh = createFn(N2);
blkdiag(Fnh);
fnd = blkdiag(Fnh,Fnh);
A = diag(W2(1:N2,1));
C = diag(W2((N2+1):end,1));
B = diag(W2(1:N2,end));
D = diag(W2((N2+1):end,end));

X = [A B;C D];

res = X * fnd * W;
t = Fnt - res;
norm(t)
subplot(2,1,1)
imagesc(abs(X))
subplot(2,1,2)
imagesc(abs(t))

x = testSignal(0,10,127);

[lp hp] = dwt(x,'db16','mode','per');
lp
hp
lpf = fft(lp);
hpf = fft(hp);
c = W*x';
c = fnd * c;
wf = X *c;
f = fft(x');
subplot(2,2,1)
plot(imag(wf));
subplot(2,2,2)
plot(imag(f));

subplot(2,2,3)
plot(real(wf));
subplot(2,2,4)
plot(real(f));

norm(f-wf)

a = [lpf hpf];

nwf = X*a';
norm(nwf/norm(nwf) -wf/norm(wf))

